import org.junit.Test;

public class StringBufferTest {
    public static void main(String[] args) {
//        String s = "bbbb";
//        StringBuffer sb = new StringBuffer("bbbb");
//        change(s, sb);
//        System.out.println(s + " " + sb); //bbbb aaaa
        String str = null;
        StringBuffer sb1 = new StringBuffer();
        sb1.append(str);
        System.out.println(sb1.length()); //4，里面做了特殊处理，遇到null会转为字符串记录
        System.out.println(sb1); //null

        StringBuffer sb2 = new StringBuffer(str);  //空指针异常
        System.out.println(sb2);
    }
    @Test
    public void test1() {
        StringBuffer s1 = new StringBuffer("abc");
        s1.append("def");
        System.out.println(s1);
        System.out.println(s1.length());
        s1.setLength(3);
        System.out.println(s1);
        s1.deleteCharAt(0);
        System.out.println(s1);
    }

    @Test
    public void test2() {
        StringBuilder s2 = new StringBuilder("abc");
        s2.append("def");
        System.out.println(s2);
        System.out.println(s2.length());
        s2.deleteCharAt(0);
        System.out.println(s2);
        s2.setLength(3);
        System.out.println(s2);
    }


    public static void change(String s1, StringBuffer s2) {
        s1 = "aaaa";
        s2.setLength(0);
        s2.append("aaaa");
    }
}
